//
//  OMOrderRejectViewController.m
//  ERP
//
//  Created by QA on 15/10/10.
//  Copyright © 2015年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "CRECOMOrderRejectViewController.h"

#import "OMOrderRejectViewCell.h"
#import "WebServiceCommonRequest.h"
#import "MJRefresh.h"

#import "UITableView+Custom.h"
#import "NSDictionary+DataFormat.h"
#import "NSString+handle.h"

@interface CRECOMOrderRejectViewController () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) NSMutableArray *resultData;
@property (nonatomic,strong) NSMutableDictionary *orderBaseInfo;

@end

@implementation CRECOMOrderRejectViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"浇筑令驳回明细"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 73;
    
    self.DDBHLable.text = [self.orderBaseInfo objectFormatStringForKey:@"订单编号" defaults:@"未知"];
    self.YSZDLable.text = [self.orderBaseInfo objectFormatStringForKey:@"原始分发站" defaults:@"未知"];
    self.SJZDLable.text = [self.orderBaseInfo objectFormatStringForKey:@"实际分发站" defaults:@"未知"];
    self.logisticsaLable.text = [self.orderBaseInfo objectFormatStringForKey:@"驳回次数" format:@"该浇筑令被驳回了%@次" defaults:@"无法获取驳回次数"];
    
    self.resultData = [NSMutableArray array];
    [self setupRefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getPassingParameter:(id)object
{
    NSMutableDictionary *dic = object;
    self.orderBaseInfo = dic;
}

- (void)setupRefresh
{
    //添加下拉加载数据
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self.tableView headerBeginRefreshing];
    [self.tableView footerEndRefreshing];
}

- (void)stopRefresh
{
    //移除下拉加载数据
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
}

#pragma mark 开始进入刷新状态

- (void)headerRereshing
{
    [self getNetWorkData];
}

#pragma mark 网络获取物流信息
- (void)getNetWorkData
{
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"DDGL_DDBH_LIST"];
    [content setResultBackType:ResultBackTypeCommonUnordered];

    NSString *DDBH = [self.orderBaseInfo objectFormatStringForKey:@"订单编号"];
    [content addParameterKey:@"订单编号" value:DDBH];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        [self.tableView headerEndRefreshing];
        [self.tableView footerEndRefreshing];
        
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
        
        [self.resultData removeAllObjects];
        if(firstArray && firstArray.count>0){
            [self.resultData addObjectsFromArray:firstArray];
            [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
            [self.tableView reloadData];
        } else {
            [self.tableView setTableViewNotDataDrawText:@"未找到驳回信息" isShow:YES];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self errorInfoViewText:@"哦，出错了"];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self errorInfoViewText:@"无法连接网络"];
    }];
    
}

- (void)errorInfoViewText:(NSString*)label
{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
    
    if (self.tableView.visibleCells.count <= 0) {
        [self.tableView setTableViewNotDataDrawText:label isShow:YES];
    } else {
        [self.tableView setTableViewNotDataDrawText:nil isShow:NO];
        [self showErrorHUDText:label];
    }
}

#pragma mark UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 30)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, SuperWidth - 10, 29.5)];
    lable.textColor = ThemeColorBlue;
    lable.font = [UIFont boldSystemFontOfSize:15];
    lable.text = [NSString stringWithFormat:@"驳回详情"];
    [view addSubview:lable];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 29.5, SuperWidth, 0.5)];
    lineView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
    [view addSubview:lineView];
    
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"OMOrderRejectViewCell%ld",(long)indexPath.row];
    OMOrderRejectViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell){
        cell = [[OMOrderRejectViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSDictionary *itemInfo = [self.resultData objectAtIndex:indexPath.row];
    cell.BHZDLabel.text = [itemInfo objectFormatStringForKey:@"驳回站" defaults:@"未知"];
    cell.BHSJLabel.text = [itemInfo objectFormatStringForKey:@"驳回时间" defaults:@"未知"];
    cell.numberLabel.text = [itemInfo objectFormatStringForKey:@"序号" defaults:@"0"];
    cell.BHYYLabel.text = [itemInfo objectFormatStringForKey:@"驳回原因" defaults:@"无"];
    
    return cell;
}

#pragma mark UITableViewDelegate

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    NSString *CellIdentifier = [NSString stringWithFormat:@"OMOrderRejectViewCell%ld",(long)indexPath.row];
//    OMOrderRejectViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    if (!cell){
//        cell = [[OMOrderRejectViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    }
//    
//    NSDictionary *itemInfo = [self.resultData objectAtIndex:indexPath.row];
//    NSString *BHYY = [itemInfo objectFormatStringForKey:@"驳回原因" defaults:@"无"];
//    CGFloat height = [BHYY heightAutoCalculateFontSize:[UIFont systemFontOfSize:13] andWidth:cell.BHYYLabel.frame.size.width];
//    
//    CGFloat superHeight = cell.frame.size.height + height - 20;
//    if (superHeight > cell.frame.size.height) return superHeight;
//    else return 73;
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark -
#pragma mark IBAction Event
- (void)backButtonEvent:(id)sender
{
    if (self.backResultDelagate && [self.backResultDelagate respondsToSelector:@selector(backResultDataKey:dataValue:)]) {
        [self.backResultDelagate backResultDataKey:self.key dataValue:nil];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
